import {createRouter, createWebHistory} from 'vue-router'
import PkIndexView from '../views/pk/PkIndexView'
import RecordIndexView from '../views/record/RecordIndexView'
import RanklistIndexVies from '../views/ranklist/RanklistIndexView'
import UserBotsIndexView from '../views/user/bots/UserBotsIndexView'
import NotFound from '../views/error/NotFound'
import RecordContentViewVue from '@/views/record/RecordContentView.vue'
import UserAccountLoginView from '../views/user/account/UserAccountLoginView'
import UserAccountRegister from '../views/user/account/UserAccountRegisterViewView'
import store from '@/store/index'

const routes = [
    {
        path: "/",
        name: "home",
        redirect: "/pk/",
        meta: {
            requestAuth: true,
        }
    },
    {
        path: "/pk/",
        name: "pk_index",
        component: PkIndexView,
        meta: {
            requestAuth: true,
        }
    },
    {
        path: "/record/",
        name: "record_index",
        component: RecordIndexView,
        meta: {
            requestAuth: true,
        }
    },
    {
        path: "/ranklist/",
        name: "ranklist_index",
        component: RanklistIndexVies,
        meta: {
            requestAuth: true,
        }
    },
    {
        path: "/user/bots/",
        name: "user_bots_index",
        component: UserBotsIndexView,
        meta: {
            requestAuth: true,
        }
    },
    {
        path: "/404/",
        name: "404",
        component: NotFound,
    },
    {
        path: "/:catchAll(.*)",
        redirect: "/404/"
    },
    {
        path: "/user/account/login/",
        name: "user_account_login",
        component: UserAccountLoginView,
    },
    {
        path: "/user/account/register/",
        name: "user_account_register",
        component: UserAccountRegister,
    },
    {
        path: "/record/:recordId/",
        name: "record_content",
        component: RecordContentViewVue,
        mate:{
            requestAuth:true,
        }
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    if (to.meta.requestAuth && !store.state.user.is_login) {
        next({name: "user_account_login"});
    } else {
        next()
    }
})

export default router
